package com.rainner.app.action;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.rainner.app.base.action.JsonAction;
import com.rainner.app.javabean.BookBean;
import com.rainner.app.javabean.BrrowedBookBean;
import com.rainner.app.javabean.ReturnBean;
import com.rainner.app.server.BookServer;
import com.rainner.app.server.BrrowedBookServer;

@Results({ @Result(name = "DELBOOK", type = "redirect", location = "/backbook.html"), @Result(name = "OUTLOGIN", type = "redirect", location = "/login.html"), @Result(name = "BACKBOOK", type = "freemarker", location = "/WEB-INF/content/public/backbook.ftl") })
public class BackBookAction extends JsonAction {
	private static final long serialVersionUID = 1L;
	private String type;
	private BrrowedBookServer brrowedBookServer = new BrrowedBookServer();
	private BookServer bookServer = new BookServer();
	private static int page = 1;
	private String value;

	@Actions({ @Action("/backbook"), @Action("/backbook" + "/{type}") })
	public String returnIndex() throws SQLException {
		HttpSession session = ServletActionContext.getRequest().getSession();
		Object username = session.getAttribute("username");
		if (username == null) {
			return "OUTLOGIN";
		} else if (type == null) {
			// 根据session查询具有什么样的书籍需要归还
			List<BrrowedBookBean> list = brrowedBookServer.listBook(username.toString());
			List<ReturnBean> bookList = new ArrayList<ReturnBean>();
			for (int i = 0; i < 5; i++) {

				BookBean bookBean = bookServer.bookDataByBookId(list.get(i).getBookID());
				ReturnBean returnBean = new ReturnBean();
				returnBean.setBookId(bookBean.getBookId());
				returnBean.setBookAuthor(bookBean.getBookAuthor());
				returnBean.setBookDate(bookBean.getBookDate());
				returnBean.setBookImg(bookBean.getBookImg());
				returnBean.setBookName(bookBean.getBookName());
				returnBean.setBookNumber(list.get(i).getBorrowedBookNumber());
				returnBean.setBookPublish(bookBean.getBookPublish());
				returnBean.setBrrowedDate(list.get(i).getBorrowedDate());

				bookList.add(returnBean);
			}
			map.put("username", username.toString());
			map.put("book", bookList);
			return "BACKBOOK";
		}
		if ("up".equals(type)) {
			List<BrrowedBookBean> list = brrowedBookServer.listBook(username.toString());
			List<ReturnBean> bookList = new ArrayList<ReturnBean>();
			if (page > 1) {
				page -= 1;
			}
			for (int i = (page - 1) * 5; i < page * 5; i++) {
				BookBean bookBean = bookServer.bookDataByBookId(list.get(i).getBookID());
				ReturnBean returnBean = new ReturnBean();

				returnBean.setBookId(bookBean.getBookId());
				returnBean.setBookAuthor(bookBean.getBookAuthor());
				returnBean.setBookDate(bookBean.getBookDate());
				returnBean.setBookImg(bookBean.getBookImg());
				returnBean.setBookName(bookBean.getBookName());
				returnBean.setBookNumber(list.get(i).getBorrowedBookNumber());
				returnBean.setBookPublish(bookBean.getBookPublish());
				returnBean.setBrrowedDate(list.get(i).getBorrowedDate());

				bookList.add(returnBean);
				map.put("username", username.toString());
				map.put("book", bookList);
			}
		} else if ("down".equals(type)) {
			List<BrrowedBookBean> list = brrowedBookServer.listBook(username.toString());
			List<ReturnBean> bookList = new ArrayList<ReturnBean>();
			if ((page + 1) * 5 < list.size() + 5) {
				page += 1;
			}
			int number = (page * 5 > list.size()) ? list.size() : page * 5;
			for (int i = (page - 1) * 5; i < number; i++) {
				BookBean bookBean = bookServer.bookDataByBookId(list.get(i).getBookID());
				ReturnBean returnBean = new ReturnBean();

				returnBean.setBookId(bookBean.getBookId());
				returnBean.setBookAuthor(bookBean.getBookAuthor());
				returnBean.setBookDate(bookBean.getBookDate());
				returnBean.setBookImg(bookBean.getBookImg());
				returnBean.setBookName(bookBean.getBookName());
				returnBean.setBookNumber(list.get(i).getBorrowedBookNumber());
				returnBean.setBookPublish(bookBean.getBookPublish());
				returnBean.setBrrowedDate(list.get(i).getBorrowedDate());

				bookList.add(returnBean);
				map.put("username", username.toString());
				map.put("book", bookList);
			}
		}
		return "BACKBOOK";
	}

	@Action("/backbook/del" + "/{value}")
	public String delBook() throws SQLException {
		HttpSession session = ServletActionContext.getRequest().getSession();
		Object username = session.getAttribute("username");
		brrowedBookServer.backBook(value, username.toString());
		return "DELBOOK";
	}

	public void setType(String type) {
		this.type = type;
	}

	public void setValue(String value) {
		this.value = value;
	}

}
